package com.cong.array;

import java.util.Arrays;

/**
 * 把数组中的0移到末尾
 *
 * 将非零的元素前移，末尾填充0
 */
public class MoveZeros {
    public static void main(String[] args) {
        int[] arr ={0,1,0,3,12};
        moveZeros(arr);
        System.out.println(Arrays.toString(arr));
    }

    static void moveZeros(int[] arr){
        int l = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i]!=0){
                //l++先计算再自增，等于arr[l]=arr[i];l++;
               arr[l++]=arr[i];
            }

        }
        while (l<= arr.length){
            arr[l++]=0;
        }
    }
}
